package com.nt.mapreduce020201_hello.yarn.wordCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.Iterator;
public class WordCountReducer extends Reducer<Text, IntWritable, Text,
        IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,
                          Context context) throws IOException, InterruptedException {
//获取迭代器
        Iterator<IntWritable> iterator = values.iterator();
//声明一个计数器
        int count = 0;
        while (iterator.hasNext()) {
            IntWritable next = iterator.next();
            System.out.println("key-->"+key+"---value-->"+next);
            count += next.get();
        }
//输出数据
        context.write(key, new IntWritable(count));
    }
}